package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.*;
import com.fantastic.model.pojo.zjs.*;
import com.fantastic.model.vo.zjs.AccountCouponsVo;
import com.fantastic.service.zjs.AccountCouponsService;
import com.fantastic.service.zjs.IntegralService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/4 08:59
 * @description: 用户优惠券关联表service接口实现类
 */
@Service
public class AccountCouponsServiceImpl implements AccountCouponsService {

    @Resource
    private AccountCouponsMapper accountCouponsMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private IntegralService integralService;

    /**
     * 查询用户优惠券关联信息
     *
     * @param accountCoupons
     * @return
     */
    @Override
    public List<AccountCoupons> listAccountCoupons(AccountCoupons accountCoupons) {
        List<AccountCoupons> accountCouponsList = accountCouponsMapper.listAccountCoupons(accountCoupons);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), accountCouponsList);
        for (AccountCoupons accountCoupon : accountCouponsList) {
            accountCoupon.getCoupons().setCouponPicture(
                    FilesUtils.getSlashParse(accountCoupon.getCoupons().getCouponPicture())
            );
        }
        return accountCouponsList;
    }

    /**
     * 查询单个用户优惠卷信息
     *
     * @param accountId
     * @return
     */
    @Override
    public AccountCouponsVo getAccountCoupons(Integer accountId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), accountId);
        AccountCouponsVo accountCoupon = accountCouponsMapper.getAccountCoupons(accountId);
        if (accountCoupon != null) {
            List<Coupons> coupons = accountCoupon.getCoupons();
            if (coupons.size() > 0) {
                for (Coupons c : coupons) {
                    c.setCouponPicture(FilesUtils.getSlashParse(c.getCouponPicture()));
                }
            }
        }
        return accountCoupon;
    }


    /**
     * 添加用户优惠券关联信息
     *
     * @param accountCoupons
     * @return
     */
    @Override
    public Integer insertAccountCoupons(AccountCoupons accountCoupons) {
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), accountCoupons.getAccountId(),
                accountCoupons.getCoupons().getCouponId());
        return accountCouponsMapper.insertAccountCoupons(accountCoupons);
    }

    /**
     * 账号兑换积分方法
     *
     * @param accountId        账号id
     * @param couponId         优惠卷id
     * @param integralExchange 所需要的积分
     * @param integralId       积分商品的id
     * @param integralSell     兑换的数量
     * @return
     */
    //开启事务
    @Transactional
    @Override
    public Integer creditsExchange(Integer accountId, Integer couponId, Integer integralExchange
            , Integer integralId, Integer integralSell) {
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.EXCHANGE_ERROR), accountId, couponId, integralExchange,
                integralId, integralSell);
        AccountCoupons accountCoupons = new AccountCoupons(accountId, new Coupons());

        //通过账号id查询到账号信息。
        Account account = accountMapper.getAccount(accountId);
        //用户id不为空，查询用户信息
        if (account.getUserId() != null) {
            User user = userMapper.getUser(account.getUserId());
            //判断用户积分是否为null
            Integer userIntegral = user.getUserIntegral();
            if (userIntegral != null) {
                //判断用户积分是否大于或是等于兑换所需的积分
                if (userIntegral >= integralExchange) {
                    //兑换逻辑：
                    user.setUserIntegral(userIntegral - integralExchange);
                    //修改原有的积分数量
                    userMapper.updateUser(user);
                    //给账号优惠卷关联对象赋值
                    accountCoupons.getCoupons().setCouponId(couponId);

                    //创建积分对象
                    Integral integral = new Integral();
                    //将积分商城id和兑换的数量设置给积分对象
                    integral.setIntegralId(integralId);
                    integral.setIntegralSell(integralSell);
                    //修改这个兑换商品的出售数量
                    integralService.updateIntegral(integral);
                } else {
                    //用户积分不足
                    throw new InsertException(RespStatusInfo.INTEGRAL_NO_SATISFY);
                }
            } else {
                //用户积分不足
                throw new InsertException(RespStatusInfo.INTEGRAL_NO_SATISFY);
            }
        } else {
            //兑换异常
            throw new InsertException(RespStatusInfo.EXCHANGE_ERROR);
        }
        return insertAccountCoupons(accountCoupons);
    }


    /**
     * 删除用户优惠券关联信息
     *
     * @param accountId
     * @param couponId
     * @return
     */
    @Override
    public Integer deleteAccountCoupons(Integer accountId, Integer couponId) {
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DATA_ERROR), accountId, couponId);
        return accountCouponsMapper.deleteAccountCoupons(accountId, couponId);
    }

}
